﻿using System;
using JetBrains.Annotations;
using JetBrains.ReSharper.Psi;

namespace MugenMvvmToolkit.ReSharper.PluginModels
{
    public struct ViewModelMetaModel
    {
        #region Constructors

        /// <summary>
        ///     Initializes a new instance of the <see cref="ViewModelMetaModel" /> class.
        /// </summary>
        public ViewModelMetaModel([NotNull] IClass @class, [NotNull] IAttributeInstance attribute)
            : this()
        {
            if (@class == null) throw new ArgumentNullException("class");
            if (attribute == null) throw new ArgumentNullException("attribute");
            
            var name = attribute.NamedParameter("Name").ConstantValue.Value as string;
            if (string.IsNullOrWhiteSpace(name))
                name = @class.GetMemberName();

            int priority = 0;
            object value = attribute.NamedParameter("Priority").ConstantValue.Value;
            if (value != null)
                int.TryParse(value.ToString(), out priority);

            ViewModelType = TypeFactory.CreateType(@class);
            Name = name;
            Priority = priority;
        }

        public ViewModelMetaModel([NotNull] IDeclaredType viewModelType, [NotNull] string name, int priority)
            : this()
        {
            if (viewModelType == null) throw new ArgumentNullException("viewModelType");
            if (name == null) throw new ArgumentNullException("name");
            ViewModelType = viewModelType;
            Priority = priority;
            Name = name;
        }

        #endregion

        #region Properties

        public IDeclaredType ViewModelType { get; private set; }

        public string Name { get; private set; }

        public int Priority { get; private set; }

        #endregion
    }
}